﻿#include "LongestPositiveMultiOfSubArr.h"


#ifdef DEV_TEST
#include <gtest/gtest.h>

class LongestPositiveMultiOfSubArrTestCase
{
public:
	std::string arrNums;

	friend std::ostream& operator<<(std::ostream& os, const LongestPositiveMultiOfSubArrTestCase& obj)
	{
		return os << "arrNums: " << obj.arrNums;
	}
};
class LongestPositiveMultiOfSubArrTest :public SolutionTestor<ILongestPositiveMultiOfSubArr, testing::TestWithParam<LongestPositiveMultiOfSubArrTestCase>>
{
protected:
	void LoadSolutions(std::vector<ILongestPositiveMultiOfSubArr*>& solutions) override
	{
		solutions = {
			new LongestPositiveMultiOfSubArr::DP,
		};
	}
};
TEST_P(LongestPositiveMultiOfSubArrTest, getLongestPositiveMutliLengthOfSubArr)
{
	TestForSolutions([](ILongestPositiveMultiOfSubArr* solution)
		{
			auto testCase = GetParam();
			std::vector<int> arr;
			FormatedUtil::parseFormated(arr, testCase.arrNums);

			//暴力遍历所有可能的子数组，得到乘积为正数的最长子数组的长度
			int longestPositiveMultiLen = 0;
			for (int i = 0; i < arr.size(); ++i)
				for (int j = i; j < arr.size(); ++j)
				{
					int multiResult = arr[i];
					for (int k = i + 1; k <= j; ++k)
						multiResult *= arr[k];

					if (multiResult > 0)
					{
						const int subArrLen = j - i + 1;
						longestPositiveMultiLen = std::max<>(subArrLen, longestPositiveMultiLen);
					}
				}

			//与solution中得到的结果进行对比
			const int result = solution->getLongestPositiveMutliLengthOfSubArr(arr);
			EXPECT_EQ(result, longestPositiveMultiLen);
		});
}

INSTANTIATE_TEST_SUITE_P(ValidityTestCases, LongestPositiveMultiOfSubArrTest, testing::Values(
	LongestPositiveMultiOfSubArrTestCase{ "[]" },
	LongestPositiveMultiOfSubArrTestCase{ "[4,7,-2,0,3,-8,2,4,-4,-2]" },
	LongestPositiveMultiOfSubArrTestCase{ "[0,0,0,0,0,0]" },
	LongestPositiveMultiOfSubArrTestCase{ "[1,2,3,-5,1]" },
	LongestPositiveMultiOfSubArrTestCase{ "[1,2,3,0,5]" }
));
#endif